#include <iostream>
#include <string>
#include <thread>
#include <unistd.h>



/*void ThreadRun(std::string name, int num)
{
    int cnt = num;
    while(cnt--)
    {
        std::cout<< name << " cnt: " << cnt << std::endl;
        sleep(1);
    }
}
int main()
{
    std::thread myThread(ThreadRun, "thread_1", 10);
    myThread.join();

    return 0;
}*/


/*void* ThreadRun(void* args)
{
    char* name = (char*)args;
    while(true)
    {
        std::cout << name << std::endl;
        sleep(1);
    }
    return nullptr;
}
int main()
{
    std::vector<pthread_t> tids;
    std::vector<char*> free;
    //创建新线程
    for(int i=0; i<5; ++i)
    {
        pthread_t tid;

        char* name = new char[20];
        free.push_back(name);
        snprintf(name, 20, "thread-%d", i+1);

        pthread_create(&tid, nullptr, ThreadRun, (void*)name);
        tids.push_back(tid);
    }
    for(int i=0; i<5; ++i)
    {
        //取消线程
        pthread_cancel(tids[i]);
        //等待线程，接收线程的返回值
        void* retval = nullptr;
        pthread_join(tids[i], &retval);
        //打印线程的返回值
        std::cout << "retval: " << (long long int)retval << std::endl;
        //释放堆上的空间
        delete[] free[i];
    }

    return 0;
}*/


/*class Num
{
public:
    int _num1;
    int _num2; 
    int _add;
    void add()
    {
        _add = _num1 + _num2;
    }
    void print()
    {
        std::cout << _num1 << " + " <<  _num2 << " = " << _add << std::endl;
    }
};
void* ThreadRun(void* args)
{
    Num* data= static_cast<Num*>(args);
    data->add();
    
    return (void*)data;
}
int main()
{
    //创建新线程
    pthread_t tid;

    Num* data = new Num;
    data->_num1 = 10;
    data->_num2 = 20;
    data->_add = 0;
    int n = pthread_create(&tid, nullptr, ThreadRun, (void*)data);
    if(n != 0)
    {
        std::cout<< "new thread create failed" << std::endl;
        exit(1);        
    }
    //主线程等待新线程
    Num* retval = nullptr;//在64位系统下地址作占大小是8个字节
    n = pthread_join(tid, (void**)&retval);
    //打印新线程函数的返回值
    if(n == 0)
    {
        ((Num*)retval)->print();
    }
    delete retval;

    return 0;
}*/


/*class ThreadData
{
public:
    std::string _thread;
    int _num;
};
void* ThreadRun(void* args)
{
    ThreadData* data= static_cast<ThreadData*>(args);
    int cnt = 5; 
    while(cnt--)
    {
        std::cout << data->_num << data->_thread <<" run cnt: " << cnt << std::endl;
        sleep(1);
    }
    return (void*)111;
}
//新线程id转换成16进制并打印
void PrintOutHex(pthread_t tid)
{
    char buffer[100];
    snprintf(buffer, sizeof(buffer), "%lx", tid);
    std::cout << "0x" << buffer << std::endl;
}
int main()
{
    //创建新线程
    pthread_t tid;

    ThreadData data;
    data._thread = "thread";
    data._num = 1;
    int n = pthread_create(&tid, nullptr, ThreadRun, (void*)&data);
    if(n != 0)
    {
        std::cout<< "new thread create failed" <<std::endl;
        exit(1);        
    }
    //主线程等待新线程
    void* retval = nullptr;//在64位系统下地址作占大小是8个字节
    n = pthread_join(tid, &retval);
    //打印新线程函数的返回值
    if(n == 0)
    {
        std::cout << "new thread return val:" << uint64_t(retval) << std::endl;
    }

    return 0;
}*/




